using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using BriansClimb;
using BriansClimb.GameObjects;
using BriansClimb.Climber;
using BriansClimb.DrawableGameObjects.Buttons;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;

namespace BriansClimb.DrawableGameObjects.GameScenes
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class VictoryScene : GameScene
    {
        protected Game game;

        protected SpriteBatch spriteBatch;

        protected Texture2D explosion;
        protected Texture2D background;
        protected Texture2D congratulations;

        protected int explosionSize0 = 30;
        protected int explosionSize1 = 30;
        protected int explosionSize2 = 30;
        protected int explosionSize3 = 30;
        protected int explosionSize4 = 30;
        protected int msgSize = 800;
        protected int flickerRate = 0;
        protected int explosionTimer = 0;                

        protected MouseState mouseState;        

        /// <summary>
        /// Default constructor
        /// Loads the textures, does not perform anything special
        /// </summary>
        /// <param name="game"></param>
        /// <param name="background"></param>
        public VictoryScene(Game game, Texture2D background)
            : base(game)
        {
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            
            this.background = background;
            this.game = game;

            explosion = game.Content.Load<Texture2D>(".\\Textures\\VictoryScene1");
            congratulations = game.Content.Load<Texture2D>(".\\Textures\\Congratulations");
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }       
        
        /// <summary>
        /// Controls the explosion flicker rate and the explosionTimer which switches the the draw method between the two options
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            flickerRate = flickerRate + 5;

            if (explosionTimer > 59 && msgSize < 1251)
            {
                msgSize = msgSize + 3;
            }
            
            if (explosionTimer < 301)
            {
                explosionTimer++;

                explosionSize0 = explosionSize0 + 5;

                if (explosionTimer > 9)
                    explosionSize1 = explosionSize1 + 5;

                if (explosionTimer > 49)
                    explosionSize2 = explosionSize2 + 5;

                if (explosionTimer > 29)
                    explosionSize3 = explosionSize3 + 5;

                if (explosionTimer > 69)
                    explosionSize4 = explosionSize4 + 5;
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// The almighty draw method. Draws 1 or 2 star explosions on the screen
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Draw(background, new Rectangle(0, 0, 1280, 1024), Color.Black);
                            
            if (flickerRate % 30 == 0)
            {
                spriteBatch.Draw(explosion, new Rectangle((600 - (explosionSize0 / 2)), (500 - (explosionSize0 / 2)), explosionSize0, explosionSize0), Color.White);
                if (explosionTimer > 9)
                    spriteBatch.Draw(explosion, new Rectangle((900 - (explosionSize1 / 2)), (900 - (explosionSize1 / 2)), explosionSize1, explosionSize1), Color.Pink);
                if (explosionTimer > 59)
                    spriteBatch.Draw(explosion, new Rectangle((250 - (explosionSize2 / 2)), (250 - (explosionSize2 / 2)), explosionSize2, explosionSize2), Color.SkyBlue);
                if (explosionTimer > 29)
                    spriteBatch.Draw(explosion, new Rectangle((900 - (explosionSize3 / 2)), (250 - (explosionSize3 / 2)), explosionSize3, explosionSize3), Color.White);
                if (explosionTimer > 69)
                    spriteBatch.Draw(explosion, new Rectangle((250 - (explosionSize4 / 2)), (900 - (explosionSize4 / 2)), explosionSize4, explosionSize4), Color.White);
            }
            else
            {
                spriteBatch.Draw(explosion, new Rectangle((600 - (explosionSize0 / 2)), (500 - (explosionSize0 / 2)), explosionSize0, explosionSize0), Color.Black);
                if (explosionTimer > 9)
                    spriteBatch.Draw(explosion, new Rectangle((900 - (explosionSize1 / 2)), (900 - (explosionSize1 / 2)), explosionSize1, explosionSize1), Color.Black);
                if (explosionTimer > 19)
                    spriteBatch.Draw(explosion, new Rectangle((250 - (explosionSize2 / 2)), (250 - (explosionSize2 / 2)), explosionSize2, explosionSize2), Color.Black);
                if (explosionTimer > 29)
                    spriteBatch.Draw(explosion, new Rectangle((900 - (explosionSize3 / 2)), (250 - (explosionSize3 / 2)), explosionSize3, explosionSize3), Color.Black);
                if (explosionTimer > 39)
                    spriteBatch.Draw(explosion, new Rectangle((250 - (explosionSize4 / 2)), (900 - (explosionSize4 / 2)), explosionSize4, explosionSize4), Color.Black);
            }
                
            spriteBatch.Draw(congratulations, new Rectangle((635 - (msgSize / 2)), (300 - (msgSize / 18)), msgSize, msgSize / 9), Color.White);            
            
            base.Draw(gameTime);
        }
    }
}